package yjxxt;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import yjxxt.Dao.AuthorsMapper;
import yjxxt.entity.Authors;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

public class main {
    public static void main(String[] args) throws IOException, SQLException {
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(inputStream);
//System.out.println(sqlSessionFactory);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        System.out.println(sqlSession);

//        DeptDao mapper = sqlSession.getMapper(DeptDao.class);
////        int row = mapper.insert(new Dept(201, "测试部", "望城区"));
//        List<DeptEmp> list = mapper.findAllDeptAndLeader();
//        for (DeptEmp dept : list) {
//            System.out.println(dept);
//        }
//        sqlSession.close();

//        SqlSession sqlSession2 = sqlSessionFactory.openSession();
//        DeptDao mapper2 = sqlSession.getMapper(DeptDao.class);
//        List<DeptEmp> list2 = mapper2.findDeptWithMostEmployees();
//        for (DeptEmp dept : list2) {
//            System.out.println(dept);
//        }
//        sqlSession2.close();

//        SqlSession sqlSession3 = sqlSessionFactory.openSession(true);
//        DeptDao mapper3 = sqlSession.getMapper(DeptDao.class);
//        boolean result = mapper.addDeptAndLeader(new Dept(10,"测试部","上海"), new Emp(1001,"小王",10,5000.0,"程序员"));
//        System.out.println(result);
//        sqlSession3.close();


////
//        SqlSession sqlSession4 = sqlSessionFactory.openSession();
//        EmpDao mapper4 = sqlSession4.getMapper(EmpDao.class);
//        List<DeptEmp> list4 = mapper4.findAllEmpWithDeptName();
//        for (DeptEmp emp : list4) {
//            System.out.println(emp);
//        }
//        sqlSession4.close();
//
//        SqlSession sqlSession5 = sqlSessionFactory.openSession();
//        EmpDao mapper5 = sqlSession5.getMapper(EmpDao.class);
//       List<Emp> list5 = mapper5.findEmpsByNameContaining(2,1,"J");
//       for (Emp emp : list5) {
//           System.out.println(emp);
//       }
//       sqlSession5.close();

//        SqlSession sqlSession6 = sqlSessionFactory.openSession();
//        EmpDao mapper6 = sqlSession6.getMapper(EmpDao.class);
//        Emp emp = mapper6.findEmpWithMaxSalary();
//        System.out.println(emp);
//        sqlSession6.close();
//
//        SqlSession sqlSession7 = sqlSessionFactory.openSession(true);
//        EmpDao mapper7 = sqlSession7.getMapper(EmpDao.class);
//        int i = mapper7.updateEmp(new Emp( 7788, "小王", "SALESMAN", 5000, "7782", "2020-01-01", 20, "500"));
//        System.out.println(i);
//        sqlSession7.close();

//        SqlSession sqlSession8 = sqlSessionFactory.openSession();
//        EmpDao mapper8 = sqlSession8.getMapper(EmpDao.class);
//        Emp emp = mapper8.findEmpById(7788);
//        System.out.println(emp);
//        sqlSession8.close();

//        SqlSession sqlSession9 = sqlSessionFactory.openSession(true);
//        EmpDao mapper9 = sqlSession9.getMapper(EmpDao.class);
//        int a = mapper9.deleteEmpById(7788);
//        System.out.println(a);
//        sqlSession9.close();
//
//        SqlSession sqlSession10 = sqlSessionFactory.openSession();
//        EmpDao mapper10 = sqlSession10.getMapper(EmpDao.class);
//        List<Emp> emp = mapper10.findEmp("MANAGER",10);
//        System.out.println(emp);
//        sqlSession10.close();

//        SqlSession sqlSession11 = sqlSessionFactory.openSession();
//        AuthorsMapper mapper11 = sqlSession11.getMapper(AuthorsMapper.class);
//        Authors authors = mapper11.selectByPrimaryKey(1);
//        System.out.println(authors);
//        sqlSession11.close();

        SqlSession sqlSession12 = sqlSessionFactory.openSession();
        AuthorsMapper mapper12 = sqlSession12.getMapper(AuthorsMapper.class);
//        int i = mapper12.updateByPrimaryKey(new Authors( 1,"豆豆","小豆","上海"));
//        System.out.println(i);
        int i = mapper12.insertSelective(new Authors(2,"小豆","小豆","上海"));
        System.out.println(i);
        sqlSession12.commit();
        sqlSession12.close();

    }
}
